package net.sashag.nadlandroid;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;

import com.google.android.apps.analytics.easytracking.TrackedActivity;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Background;
import com.googlecode.androidannotations.annotations.Bean;
import com.googlecode.androidannotations.annotations.EActivity;
import com.googlecode.androidannotations.annotations.Extra;
import com.googlecode.androidannotations.annotations.UiThread;
import com.googlecode.androidannotations.annotations.ViewById;
import com.parse.ParseException;
import com.parse.ParseUser;

@EActivity(R.layout.gridaptdetails)
public class GridApartmentListingActivity extends TrackedActivity {

	private static final int REMOVE_FROM_FAVORITES_MENU_ID = 1;
	
	@Extra("ApartmentIds") protected String[] apartmentIds;
	@Extra("Title") protected String title;
	@Extra("Apartments") protected List<Apartment> apartments;
	@Extra("Favorites") protected boolean isShowingFavorites;
	
	@ViewById protected ListView listApartments;
	
	private ProgressDialog progress;
	private ApartmentAdapter apartmentsAdapter;
	
	@Bean protected FavoritesManager favoritesManager;
	
	@AfterViews
	protected void populateApartments() {
		if (title != null && title.length() > 0) {
			setTitle(title);
		}
		if (apartments != null) {
			finalSetAdapter(apartments);
		} else {
			if (apartmentIds == null) {
				apartmentIds = new String[0];
			}
			progress = new ProgressDialog(this);
			progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			progress.setTitle("Fetching Apartments");
			progress.setMessage("Waiting for apartment information from our web service...");
			progress.setCancelable(false);
			progress.show();
			fetchApartmentObjects();
		}
		if (isShowingFavorites) {
			//Add a context menu for deleting favorites.
			registerForContextMenu(listApartments);
		}
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
		Apartment apartment = apartmentsAdapter.getItem(info.position);
		menu.setHeaderTitle(apartment.getAddress());
		menu.add(Menu.NONE, REMOVE_FROM_FAVORITES_MENU_ID, Menu.NONE, "Remove from Favorites");
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		if (item.getItemId() != REMOVE_FROM_FAVORITES_MENU_ID) {
			return false;
		}
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		Apartment apartment = apartmentsAdapter.getItem(info.position);
		apartmentsAdapter.remove(apartment);
		apartmentsAdapter.notifyDataSetChanged();
		favoritesManager.removeFromFavorites(apartment.getObjectId(), ParseUser.getCurrentUser());
		return true;
	}
	
	@Background
	protected void fetchApartmentObjects() {
		List<Apartment> apartments = new ArrayList<Apartment>();
		for (String apartmentId : apartmentIds) {
			try {
				apartments.add(Apartment.fromId(apartmentId));
			} catch (ParseException e) {
				Log.w("GridApartments", "Error fetching apartment " + apartmentId + ": " + e.getMessage());
			}
		}
		finalSetAdapter(apartments);
	}
	
	@UiThread
	protected void finalSetAdapter(List<Apartment> apartments) {
		apartmentsAdapter = new ApartmentAdapter(this, apartments);
		listApartments.setAdapter(apartmentsAdapter);
		progress.dismiss();
	}
}
